import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class FileIO {

	
	public String createProcessFile(String fileName) {
		String filePath=null;
		File f = new File(fileName);
		if(!f.exists()){
			try {
				f.createNewFile();
				filePath =f.getAbsolutePath();
			}
			catch (IOException e) {
				System.out.println("failed : process file directory ");
				return filePath;
			}
			System.out.println("success: processed file directory created "+fileName);
		}
		filePath =f.getAbsolutePath();
		return filePath;
	}
	

	
	public void writeToFile(String text, String outFilePath)
	{
		File outFile = new File(outFilePath);
		Writer out = null;
		try
		{
			out = new BufferedWriter(new FileWriter(outFile));
			out.write(text);
			out.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
	}

	
	public StringBuffer readFromFile(File f){
		String line=null;
		StringBuffer contents = new StringBuffer();
		if(f!=null){
			try {
				BufferedReader read = new BufferedReader(new FileReader(f));
				while((line =read.readLine())!=null){
					contents.append(line);
					contents.append("\n");
				}
			} 
			catch (FileNotFoundException e) {
				System.out.println("Failed : file not found, while reading the contents of the article "+e);				
			} catch (IOException e) {
				System.out.println("Failed : IO error , while reading the contents of the article "+e);
			}
		}
		return contents; 
	}

}
